//手写Promise.all
function PromiseAll(promiseArray) {
  //请在此处补全代码
  return new Promise((resolve, reject) => {
    const res = [];
    let counter = 0;
    for (let i = 0; i < promiseArray.length; i++) {
      Promise.resolve(promiseArray[i])
        .then((value) => {
          res[i] = value;
          // console.log(value);
          counter++;
          if (counter === promiseArray.length) resolve(res);
        })
        .catch((reason) => reject(reason));
    }
  });
}

const p1 = new Promise((res, rej) => {
  setTimeout(() => {
    res("p1");
  }, 1000);
});

const p2 = new Promise((res, rej) => {
  setTimeout(() => {
    res("p2");
  }, 2000);
});

const p3 = new Promise((res, rej) => {
  setTimeout(() => {
    res("p3");
  }, 3000);
});

const test = PromiseAll([p1, p2, p3])
  .then((res) => console.log(res))
  .catch((e) => console.log(e));

// console.log(test);
